package com.emip.servlet.report;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.emip.bean.SecondStoreBean;
import com.emip.bean.SiteItemBean;
import com.emip.pojo.Report;
import com.emip.pojo.Table;
import com.emip.service.SecondStoreService;
import com.emip.util.TableFormat;


@Controller
@RequestMapping(value="/report/original")
public class OriginalReportAction extends ReportAction {

	@Autowired
	SecondStoreService secondStoreService;
	
	@RequestMapping(value="/show")
	public ModelAndView index() {
		return new ModelAndView("/report/report", setDefaultParams(mmap));
	}
	
	@RequestMapping(value="/data", method=RequestMethod.POST)
	@ResponseBody
	public Table<Map<String,Object>> getRealData(
			@RequestParam("startDate") String startDate, 
			@RequestParam("startHour") String startHour,
			@RequestParam("endDate") String endDate,
			@RequestParam("endHour") String endHour,
			@RequestParam("startMinute") String startMinute,
			@RequestParam("endMinute") String endMinute,
			@RequestParam("siteId") String siteId,
			@RequestParam("itemIds[]") String[] itemIds,
			@RequestParam("siteItems[]") int[] siteItemIds,
			@RequestParam("page") int page,
			@RequestParam("rows") int size) 
			throws ParseException {
		startDate += " " + startHour + ":" + startMinute + ":00";
		endDate += " " + endHour + ":" + endMinute + ":59";
		List<SecondStoreBean> result = secondStoreService.getByTimespan(startDate, endDate, itemIds, siteId, page, size);
		
		for (int i = 0; i < result.size(); i++) {
			SecondStoreBean sec = result.get(i);
			sec.setValue(sec.getOriValue());
		}
		
		long records = secondStoreService.getCount(startDate, endDate, itemIds, siteId);
		long totalPages = records / size + (records % size == 0 ? 0 : 1);
		Report report = formatReportBody(result);
		return new TableFormat<Map<String, Object>>().formTable(totalPages, page, records, report.getBodys());
	}
	
	@RequestMapping(value="/statistic", method=RequestMethod.POST)
	@ResponseBody
	public Report getStatistic(
			@RequestParam("startDate") String startDate, 
			@RequestParam("startHour") String startHour,
			@RequestParam("endDate") String endDate,
			@RequestParam("endHour") String endHour,
			@RequestParam("startMinute") String startMinute,
			@RequestParam("endMinute") String endMinute,
			@RequestParam("siteId") String siteId,
			@RequestParam("itemIds") String[] itemIds,
			@RequestParam("siteItems") int[] siteItemIds,
			HttpSession session) 
			throws ParseException {
		startDate += " " + startHour + ":" + startMinute + ":00";
		endDate += " " + endHour + ":" + endMinute + ":59";
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = df.parse(startDate);
		Date end = df.parse(endDate);
		Report report = null;
		if ((end.getTime() - start.getTime()) / 3600*1000 < 24) {
			List<SecondStoreBean> result = 
					secondStoreService.getByTimespan(startDate, endDate, itemIds, siteId);
			for (int i = 0; i < result.size(); i++) {
				SecondStoreBean sec = result.get(i);
				sec.setValue(sec.getOriValue());
			}
			report = formatReport(result, siteItemIds, session);
		} else {
			Map<String, String[]> stats = new HashMap<String, String[]>();
			List<SiteItemBean> siteItems = new ArrayList<SiteItemBean>();
			for (int i = 0; i < siteItemIds.length; i++) {
				SiteItemBean siteItem = siteItemService.getSiteItemByID(siteItemIds[i]);
				siteItems.add(siteItem);
				String itemId = siteItem.getItem().getId();
				stats.put(siteId + "_" + itemId, secondStoreService.getStatistics(startDate, endDate, itemId, siteId));
				session.setAttribute(getCurName()+"ReportProgress", 100*i/(siteItemIds.length+1));
			}
			report = formatReport(stats, siteItems, session);
		}
		return report;
	}
}
